
import 'package:flutter/services.dart';

class VpnService {
  static const _channel = MethodChannel('com.example.vpn');
  static const _eventChannel = EventChannel('com.example.vpn/events');

  static Future<bool> connect({
    String entryName = 'FlutterVPN',
    required String username,
    required String password,
  }) async {
    try {
      return await _channel.invokeMethod('connect',  {
        'entryName': entryName,
        'username': username,
        'password': password,
      });
    } on PlatformException catch (e) {
      throw _parseError(e);
    }
  }

  static Stream<String> get vpnStatus => _eventChannel
      .receiveBroadcastStream()
      .map((event) => event.toString());

  static String _parseError(PlatformException e) {
    switch (e.code)  {
      case 'ERROR_691':
        return '认证失败: ${e.message}';
      default:
        return e.message  ?? '未知错误';
    }
  }
}